﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Library.Business;
using NhatThanhSoftware;
using Library.Controller;
using CommonControl;

namespace HoaDonNuoc.Interface.ThongTinGia
{
    public partial class frmThongTinGia : DevExpress.XtraEditors.XtraForm
    {
        public frmThongTinGia()
        {
            InitializeComponent();
        }

        // ------- load data ----------------
        private void frmThongTinGia_Load(object sender, EventArgs e)
        {
            ThuVienDevExpress.SetFormatForGridView(grvThongTinGia_ChiTiet);
            this.Text = Cls_ThongTinGia_ChiTiet.str_NameTable_Description;
            //ThuVienDevExpress.SetDisplayNumber("#,##0.00", col_DonGia);


           // ThuVienDevExpress.SetDisplayNumber
            
            Set_Caption_grv();
        }
        

        /// <summary>
        /// Set cation for Gridview.
        /// </summary>
        DataTable data; 
        private void Set_Caption_grv()
        {
            col_ID.Caption = Cls_ThongTinGia_ChiTiet.fn_ID_MoTa;
            col_ID_ThongTinGia.Caption = Cls_ThongTinGia_ChiTiet.fn_ID_ThongTinGia_MoTa;
            col_ID_LoaiHinhDoanhNghiep.Caption = Cls_ThongTinGia_ChiTiet.fn_ID_LoaiHinhDoanhNghiep_MoTa;
            col_ID_LoaiHinhDoanhNghiep.Width = Cls_ThongTinGia_ChiTiet.fn_ID_LoaiHinhDoanhNghiep_Width;
            //col_ID_LoaiHinhDoanhNghiep.ColumnEdit = new InitComboGrv().LoaiHinhDoanhNghiep_Display();
            col_ID_LoaiHinhDoanhNghiep.OptionsColumn.AllowEdit = false;
            col_ID_LoaiHinhDoanhNghiep.OptionsColumn.AllowFocus = false;


            col_ID_HeThongNuoc.Caption = "Hệ thống";
          //  col_ID_HeThongNuoc.ColumnEdit = new InitComboGrv().HeThongNuoc_Display();

            //
            col_DonGia.Caption = Cls_ThongTinGia_ChiTiet.fn_DonGia_MoTa;
            col_DonGia.Width = Cls_ThongTinGia_ChiTiet.fn_DonGia_Width;
            col_DonGia.ColumnEdit = new ThuVienDevExpress().MaskEditInput_Number(6, 2);

            col_ThueGTGT.Caption = Cls_ThongTinGia_ChiTiet.fn_ThueGTGT_MoTa;
            col_ThueGTGT.Width = Cls_ThongTinGia_ChiTiet.fn_ThueGTGT_Width;
            col_ThueGTGT.ColumnEdit = new ThuVienDevExpress().MaskEditInput_Number(2, 0);

            col_Thue_1.Caption = Cls_ThongTinGia_ChiTiet.fn_Thue_1_MoTa;
            col_Thue_1.Width = Cls_ThongTinGia_ChiTiet.fn_Thue_1_Width;
            col_Thue_1.ColumnEdit = new ThuVienDevExpress().MaskEditInput_Number(2, 0);

            col_Thue_2.Caption = Cls_ThongTinGia_ChiTiet.fn_Thue_2_MoTa;
            col_Thue_2.Width = Cls_ThongTinGia_ChiTiet.fn_Thue_2_Width;
            col_Thue_2.ColumnEdit = new ThuVienDevExpress().MaskEditInput_Number(2, 0);

            col_Phi_1.Caption = Cls_ThongTinGia_ChiTiet.fn_Phi_1_MoTa;
            col_Phi_1.Width = Cls_ThongTinGia_ChiTiet.fn_Phi_1_Width;
            col_Phi_1.ColumnEdit = new ThuVienDevExpress().MaskEditInput_Number(2, 0);

            col_Hidden.Caption = Cls_ThongTinGia_ChiTiet.fn_Hidden_MoTa;
        }
      
       
        private void ThongTinGia_Load()
        {
            data = Get_Procerure.AUC_st_ThongTinGia_Tao(txtChonThangNam1.Month, txtChonThangNam1.Year);
            grcThongTinGia_ChiTiet.DataSource = data;
            grvThongTinGia_ChiTiet.ExpandAllGroups();
        }
        //  ------ end load data ----------------

        private void txtChonThangNam1_EditValueChanged(object sender, EventArgs e)
        {
            ThongTinGia_Load();
        }
        private bool Check_Data_Grid()
        {
            bool ketQua = true;
            for (int i = 0; i < grvThongTinGia_ChiTiet.RowCount; i++)
            {
                DataRow dr = grvThongTinGia_ChiTiet.GetDataRow(i);//lấy hàng hiện tại.
                if (dr == null) continue;//nếu rỗng bỏ qua.
                if (dr[Cls_ThongTinGia_ChiTiet.fn_ID_ThongTinGia].ToString().Trim() == string.Empty)
                {
                    ketQua = false;
                    grvThongTinGia_ChiTiet.SelectRow(i);
                    grvThongTinGia_ChiTiet.FocusedColumn = col_ID_ThongTinGia;
                    grcThongTinGia_ChiTiet.Focus();
                    ThongBao.msgCanhBao("Chưa nhập " + Cls_ThongTinGia_ChiTiet.fn_ID_ThongTinGia_MoTa);
                    break;
                }
               
                if ( Convert.ToDouble( dr[Cls_ThongTinGia_ChiTiet.fn_DonGia])<0)
                {
                    ketQua = false;
                    grvThongTinGia_ChiTiet.SelectRow(i);
                    grvThongTinGia_ChiTiet.FocusedColumn = col_DonGia;
                    grcThongTinGia_ChiTiet.Focus();
                    ThongBao.msgCanhBao(Cls_ThongTinGia_ChiTiet.fn_DonGia_MoTa+" không được âm !");
                    break;
                }
                if (Convert.ToDouble(dr[Cls_ThongTinGia_ChiTiet.fn_ThueGTGT])<0)
                {
                    ketQua = false;
                    grvThongTinGia_ChiTiet.SelectRow(i);
                    grvThongTinGia_ChiTiet.FocusedColumn = col_ThueGTGT;
                    grcThongTinGia_ChiTiet.Focus();
                    ThongBao.msgCanhBao(Cls_ThongTinGia_ChiTiet.fn_ThueGTGT_MoTa + " không được âm !");
                    break;
                }
                if (Convert.ToDouble(dr[Cls_ThongTinGia_ChiTiet.fn_Thue_1])<0)
                {
                    ketQua = false;
                    grvThongTinGia_ChiTiet.SelectRow(i);
                    grvThongTinGia_ChiTiet.FocusedColumn = col_Thue_1;
                    grcThongTinGia_ChiTiet.Focus();
                    ThongBao.msgCanhBao(Cls_ThongTinGia_ChiTiet.fn_Thue_1_MoTa + " không được âm !");
                    break;
                }
                if (Convert.ToDouble(dr[Cls_ThongTinGia_ChiTiet.fn_Thue_2])<0)
                {
                    ketQua = false;
                    grvThongTinGia_ChiTiet.SelectRow(i);
                    grvThongTinGia_ChiTiet.FocusedColumn = col_Thue_2;
                    grcThongTinGia_ChiTiet.Focus();
                    ThongBao.msgCanhBao(Cls_ThongTinGia_ChiTiet.fn_Thue_2_MoTa + " không được âm !");
                    break;
                }

                if (Convert.ToDouble(dr[Cls_ThongTinGia_ChiTiet.fn_Phi_1]) < 0)
                {
                    ketQua = false;
                    grvThongTinGia_ChiTiet.SelectRow(i);
                    grvThongTinGia_ChiTiet.FocusedColumn = col_Phi_1;
                    grcThongTinGia_ChiTiet.Focus();
                    ThongBao.msgCanhBao( Cls_ThongTinGia_ChiTiet.fn_Phi_1_MoTa + " không được âm !");
                    break;
                }
                
            }
            return ketQua;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (!Check_Data_Grid()) return;
            Controller_ThongTinGia_ChiTiet.doCHECK(data);
            bool save_ThongTinGia_ChiTiet = Controller_ThongTinGia_ChiTiet.doSAVE(data);
            if (save_ThongTinGia_ChiTiet)
            {
                ThongBao.msgLuuThanhCong(Controller_ThongTinGia_ChiTiet.str_NameTable_Description);
            }
            else
                ThongBao.msgDuLieuChuaThayDoi();
            //
            data.AcceptChanges();
            grvThongTinGia_ChiTiet.Focus();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btNapLai_Click(object sender, EventArgs e)
        {
            ThongTinGia_Load();
        }
        public object getThongTinGia(DataTable dt, string tenTruong, string idHeThongNuoc, string idLoaiHinhDN)
        {
            try
            {
                string sql = Cls_ThongTinGia.fn_ID_HeThongNuoc + "='"+idHeThongNuoc+"' AND "
                    + Cls_ThongTinGia_ChiTiet.fn_ID_LoaiHinhDoanhNghiep + "='"+ idLoaiHinhDN+"'";
                DataRow[] dr = dt.Select(sql);
                if (dr.Length > 0)
                    return dr[0][tenTruong];
                else
                    return string.Empty;
            }
            catch
            {
                return 0;
            }
        }
        private void btTaoGiongThangTruoc_Click(object sender, EventArgs e)
        {
            //Bước 1: đang tháng 4,
//            Get_Procerure.AUC_st_ThongTinGia_Tao(txtChonThangNam1.Month, txtChonThangNam1.Year);
            // DataTable dt_Thang_Truoc = ....
            DataTable dt_ThangTruoc = Get_Procerure.AUC_st_ThongTinGia_Tao(txtChonThangNam1.Month-1, txtChonThangNam1.Year);
            // Xóa dữ liệu tháng 4 - tbthongTinGia & Chi tiet where Thang=? & Nam=?
            int delete_ThongTinGia = Cls_ThongTinGia.xoa_by_Thang_Nam(txtChonThangNam1.Month, txtChonThangNam1.Year);
           // bool delete_ThongTinGia = Controller_ThongTinGia_ChiTiet.
            // Forechr(dt_Thang_Truoc)

            Cls_HeThongNuoc[] arr_HeThongNuoc = Cls_HeThongNuoc.getArrayObject();
            Cls_LoaiHinhDoanhNghiep[] arr_LoaiHinhDoanhNghiep = Cls_LoaiHinhDoanhNghiep.getArrayObject();

            foreach (Cls_HeThongNuoc htn in arr_HeThongNuoc)
            {
                Cls_ThongTinGia _ThongTinGia = new Cls_ThongTinGia();
                _ThongTinGia.ID_HeThongNuoc = htn.ID_HeThongNuoc;
                _ThongTinGia.Thang = txtChonThangNam1.Month;
                _ThongTinGia.Nam = txtChonThangNam1.Year;
                _ThongTinGia.Hidden = false;
                _ThongTinGia.doInsert();
                int _idMAX_TTG = Cls_ThongTinGia.getMaxIdAuto();
                //
                foreach (Cls_LoaiHinhDoanhNghiep lhdn in arr_LoaiHinhDoanhNghiep)
                {
                    Cls_ThongTinGia_ChiTiet _ThongTinGia_ChiTiet = new Cls_ThongTinGia_ChiTiet();
                    _ThongTinGia_ChiTiet.ID_ThongTinGia = _idMAX_TTG;
                    _ThongTinGia_ChiTiet.ID_LoaiHinhDoanhNghiep = lhdn.ID_LoaiHinhDoanhNghiep;

                    _ThongTinGia_ChiTiet.DonGia = Convert.ToDouble( getThongTinGia(dt_ThangTruoc,Cls_ThongTinGia_ChiTiet.fn_DonGia,htn.ID_HeThongNuoc,lhdn.ID_LoaiHinhDoanhNghiep) );
                    _ThongTinGia_ChiTiet.ThueGTGT = Convert.ToDouble(getThongTinGia(dt_ThangTruoc, Cls_ThongTinGia_ChiTiet.fn_ThueGTGT, htn.ID_HeThongNuoc, lhdn.ID_LoaiHinhDoanhNghiep));
                    _ThongTinGia_ChiTiet.Thue_1 = Convert.ToDouble(getThongTinGia(dt_ThangTruoc, Cls_ThongTinGia_ChiTiet.fn_Thue_1, htn.ID_HeThongNuoc, lhdn.ID_LoaiHinhDoanhNghiep));
                    _ThongTinGia_ChiTiet.Thue_2 = Convert.ToDouble(getThongTinGia(dt_ThangTruoc, Cls_ThongTinGia_ChiTiet.fn_Thue_2, htn.ID_HeThongNuoc, lhdn.ID_LoaiHinhDoanhNghiep));
                    _ThongTinGia_ChiTiet.Thue_3 = 0;
                    _ThongTinGia_ChiTiet.Phi_1 = Convert.ToDouble(getThongTinGia(dt_ThangTruoc, Cls_ThongTinGia_ChiTiet.fn_Phi_1, htn.ID_HeThongNuoc, lhdn.ID_LoaiHinhDoanhNghiep));
                    _ThongTinGia_ChiTiet.Phi_2 = 0;
                    _ThongTinGia_ChiTiet.Phi_3 = 0;
                    _ThongTinGia_ChiTiet.Hidden = false;
                    _ThongTinGia_ChiTiet.doInsert();
                }

            }
            ThongTinGia_Load();
            // Tạo bảng ghi hóa đơn nước - 
           // Cls_ThongTinGia _HDN = new Cls_ThongTinGia();
            //_HDN.ID_HeThongNuoc = "";
            ////
            //Cls_ThongTinGia_ChiTiet _HDN_CT = new Cls_ThongTinGia_ChiTiet();
            //



        }
    }
}